<template>
  <!-- 对话框(添加 / 修改) -->
  <el-dialog
    :title="title"
    :visible.sync="show"
    width="500px"
    append-to-body
    :before-close="cancel"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <!-- <el-form-item label="编号" prop="classesNumber">
        <el-input v-model="form.classesNumber" placeholder="请输入编号" />
      </el-form-item> -->
      <el-form-item label="班级名称" prop="classesName">
        <el-input v-model="form.classesName" placeholder="请输入班级名称" />
      </el-form-item>
      <el-form-item label="年级" prop="classesGrade">
        <el-select v-model="form.classesGrade" placeholder="请选择年级">
          <el-option
            v-for="dict in this.getDictDatas(DICT_TYPE.CLSSS_PROPERTIES)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="班级代码" prop="classesCode">
        <el-input v-model="form.classesCode" placeholder="请输入班级代码" />
      </el-form-item> -->
      <el-form-item label="学院" prop="collegeId">
        <college-selector v-model="form.collegeId"></college-selector>
      </el-form-item>
      <el-form-item label="系部" prop="departmentsId">
        <departments-selector
          v-model="form.departmentsId"
          :college-id="form.collegeId"
        ></departments-selector>
      </el-form-item>
      <el-form-item label="专业" prop="professionId">
        <professions-selector
          v-model="form.professionId"
          :department-id="form.departmentsId"
        ></professions-selector>
      </el-form-item>

      <el-form-item label="开班时间" prop="openingTime">
        <el-date-picker
          clearable
          v-model="form.openingTime"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="选择开班时间"
        />
      </el-form-item>

      <el-form-item label="班主任" prop="classTeacherName">
        <!-- <el-input v-model="form.classTeacherName" placeholder="请输入班主任" /> -->
        <teacher-selector
          v-model="form.classTeacherName"
          :college-id="form.collegeId"
        ></teacher-selector>
      </el-form-item>

      <el-form-item label="辅导员" prop="classInstructorName">
        <!-- <el-input
          v-model="form.classInstructorName"
          placeholder="请输入辅导员"
        /> -->
        <teacher-selector
          v-model="form.classInstructorName"
          :college-id="form.collegeId"
        ></teacher-selector>
      </el-form-item>
      <el-form-item
        label="考勤审批人"
        prop="teacherUserType"
        label-width="100px"
      >
        <el-radio-group v-model="form.teacherUserType">
          <el-radio
            v-for="item in teacherTypeList"
            :key="item.value"
            :label="parseInt(item.value)"
            >{{ item.label }}</el-radio
          >
        </el-radio-group>
      </el-form-item>
      <!-- <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入备注" />
      </el-form-item> -->
      <el-form-item label="班级简介" prop="remark">
        <editor v-model="form.remark" :min-height="192" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { createClasses, updateClasses } from "@/api/collegebase/classes";
import Editor from "@/components/Editor";
import common from "@/api/collegebase/common";
import CollegeSelector from "@/components/CollegeSelector";
import DepartmentsSelector from "@/components/DepartmentsSelector";
import ProfessionsSelector from "@/components/ProfessionsSelector";
import TeacherSelector from "@/components/TeacherSelector";
export default {
  name: "ClassesEdit",
  components: {
    ProfessionsSelector,
    DepartmentsSelector,
    CollegeSelector,
    Editor,
    TeacherSelector
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // classesCode:[
        //    { required: true, message: '请输入班级代码', trigger: 'blur' },
        // ],

        classesName: [
          { required: true, message: '请输入班级名称', trigger: 'blur' },
        ],
        classesGrade: [
          { required: true, message: '请选择年级', trigger: 'blur' },
        ],
        collegeId: [
          { required: true, message: '请选择学院', trigger: 'blur' },
        ],
        departmentsId: [
          { required: true, message: '请选择系部', trigger: 'blur' },
        ],
        professionId: [
          { required: true, message: '请选择专业', trigger: 'blur' },
        ],
        openingTime: [
          { required: true, message: '请选择开班时间', trigger: 'blur' },
        ],
        classTeacherName: [
          { required: true, message: '请选择班主任', trigger: 'blur' },
        ],
        classInstructorName: [
          { required: true, message: '请选择辅导员', trigger: 'blur' },
        ],
      },
      teacherTypeData: this.getDictDatas('classes_teacher_type')
    };
  },
  computed: {
    teacherTypeList() {
      return this.teacherTypeData.filter((item) => {
        return item.value != "3"
      })
    }
  },
  created() {
    this.getData();
  },
  props: {
    // 是否显示弹出层
    show: {
      type: Boolean,
      default: false,
    },
    // 显示弹出层标题
    title: {
      type: String,
      default: "",
    },
  },
  methods: {


    //获取学院
    async getData() {

    },
    /** 初始化编辑的数据**/
    initData(rowData) {
      this.reset();
      if (rowData && rowData.id) {
        this.form = rowData;
        this.form.datetimerange = [this.form.startTime, this.form.endTime];
      }
    },
    /** 取消按钮 */
    cancel() {
      this.$emit("update:show", false);
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        classesNumber: undefined,
        classesName: undefined,
        classesCode: undefined,
        collegeId: undefined,
        departmentsId: undefined,
        professionId: undefined,
        classesGrade: undefined,
        openingTime: undefined,
        orgId: undefined,
        remark: undefined,
        teacherUserType: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateClasses(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.cancel();
            this.$emit("success");
          });
          return;
        }
        // 添加的提交
        createClasses(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.cancel();
          this.$emit("success");
        });
      });
    },
  },
};
</script>
